package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;



import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
//    @Autowired
//    private ItemClient itemClient;

@GetMapping("/list")
  public PageDTO<Item> querypage(@RequestParam("page") Integer page,@RequestParam("size") Integer size) {
    //new出一个 dot
    PageDTO<Item> pageDTO = new PageDTO<>();
    //new出一个list集合
    List<Item> items = new ArrayList<>();


    Page<Item> itemPage = new Page<>(page,size);
    Page<Item> page1 = itemService.page(itemPage);
    List<Item> records = page1.getRecords();
    //怎么把page存到list集合中

    pageDTO.setTotal(page1.getTotal());
    pageDTO.setList(records);

    return pageDTO;
}
@GetMapping("/{id}")
public Item selectByid(@PathVariable("id") Long id){

  Item byId = itemService.getById(id);

  return byId;

}
@PostMapping
public void insert(@RequestBody Item item){
  //Date date=new Date();
  LocalDateTime localDateTime = LocalDateTime.now();
//获取系统默认时区
  ZoneId zoneId = ZoneId.systemDefault();
//时区的日期和时间
  ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
//获取时刻
  Date date = Date.from(zonedDateTime.toInstant());

  item.setCreateTime(date);
  item.setUpdateTime(date);
  itemService.save(item);
 // List<Item> list = itemService.list();

}
@PutMapping("/status/{id}/{status}")
public void updatestatus(@PathVariable("id") Long id,Item item){
  Item item1 = new Item();
  item1.setStatus(item.getStatus());
  boolean b = itemService.updateById(item);
}
@DeleteMapping("/{id}")
public void updateAll(@PathVariable Long id){
  boolean b = itemService.removeById(id);
}
@PutMapping
public void update(@RequestBody Item item){

  LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();

  itemService.updateById(item);


}

}
